package com.example.szh.mvp.view.video;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.szh.R;
import com.example.szh.adapter.RecommendAdapter;
import com.example.szh.entity.BaseEntity;
import com.example.szh.entity.RecommendEntity;
import com.example.szh.http.HttpCode;
import com.example.szh.mvp.contract.LREContract;
import com.example.szh.mvp.di.DaggerLREConponent;
import com.example.szh.mvp.di.LREMoudes;
import com.example.szh.mvp.presenter.LREPresenter;
import com.example.szh.nextwork.view.BaseFragment;
import com.example.szh.utils.ToastUtils;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ReCommendFragment extends BaseFragment<LREPresenter> implements LREContract.ILREView,BaseQuickAdapter.OnItemChildClickListener {
    private RecyclerView recommend_rv;
    private LinearLayoutManager manager;
    private RecommendAdapter adapter;
    private SmartRefreshLayout recommendSmart;


    @Override
    public int bindLayout() {
        return R.layout.fragment_re_commend;
    }

    @Override
    public void initView() {
        recommend_rv = f(R.id.recommend_rv);
        manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recommend_rv.setLayoutManager(manager);
        adapter = new RecommendAdapter();
        adapter.setOnItemChildClickListener(this);
        recommend_rv.setAdapter(adapter);
        recommendSmart = f(R.id.recommend_smart);
        recommendSmart.setRefreshFooter(new ClassicsFooter(getActivity()));
        recommendSmart.setRefreshHeader(new ClassicsHeader(getActivity()));
        recommendSmart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh();
            }
        });
    }

    @Override
    public void initData() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", HttpCode.RECOMMENDNEWSCODE);
        p.requestAll(map);
    }

    @Override
    public void inject() {
        DaggerLREConponent.builder().lREMoudes(
                new LREMoudes(this)).build().injectRecommendFragment(this);
    }

    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof RecommendEntity) {
            RecommendEntity recommendEntity = (RecommendEntity) entity;
            adapter.setNewData(recommendEntity.getValues());
        }
    }

    @Override
    public void refreshRecyckerView(BaseEntity entity) {

    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {

    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        List<RecommendEntity.Values> list = (List<RecommendEntity.Values>) adapter.getData();
        ToastUtils.showMsg("当前点击:" + list.get(position).news_title);
    }
}